package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.QueryDeviceDataHistoryDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 老人家属Controller
 * 
 * @author laoYe
 * @date 2025-10-27
 */
@Slf4j
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属相关接口")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IDeviceDataService deviceServiceData;

    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public R login(@RequestBody UserLoginRequestDto dto){
        LoginVo vo = familyMemberService.login(dto);
        return R.ok(vo);
    }

    @PostMapping("/add")
    @ApiOperation("绑定老人")
    public R addFamily(@RequestBody FamilyMemberElder dto){
        familyMemberService.addFamily(dto);
        return R.ok();
    }

    @GetMapping("/my")
    @ApiOperation("查询绑定家人")
    public R getMyFamily(){
        List<FamilyBindResponseVO> listvo = familyMemberService.getMyFamilies();
        return R.ok(listvo);
    }

    /**
     * 查询健康数据
     * @param iotId
     * @return
     */
    @ApiOperation("查询健康数据")
    @GetMapping("/queryServiceProperties/{iotId}")
    public Map<String, Object> queryDeviceProperties(@PathVariable String iotId) {


        List<DeviceShadowDataVo> properties = deviceService.queryServiceProperties(iotId);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "操作成功");
        response.put("data", properties);

        return response;
    }

    @ApiOperation("分页查询")
    @GetMapping("list-by-page")
    public R<List<FamilyListVo>> listByPage(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ){
        List<FamilyListVo> list = familyMemberService.listByPage(pageNum, pageSize);
        return R.ok(list);
    }

    /**
     * 按天统计查询指标数据
     * @param dto
     * @return
     */
    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation("按天统计查询指标数据")
    public R<List<DeviceFunctionDataVo>> queryDeviceDataListByDay(QueryDeviceDataHistoryDto dto) {
        List<DeviceFunctionDataVo> deviceDataListByDay = deviceServiceData.queryDeviceDataListByDay(dto);
        return R.ok(deviceDataListByDay);
    }

    @GetMapping("/queryDeviceDataListByWeek")
    @ApiOperation("按周统计查询指标数据")
    public R <List<DeviceFunctionDataVo>> queryDeviceDataListByWeek(QueryDeviceDataHistoryDto dto) {
        List<DeviceFunctionDataVo> deviceDataListByWeek = deviceService.queryDeviceDataListByWeek(dto);
        return R.ok(deviceDataListByWeek);
    }
}
